package org.maxgroundstation.simulation;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * Static utility class for the MAX robot simulator.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class MaxRobotUtil {

    /**
     * Private constructor for utility class.
     */
    private MaxRobotUtil() {}
    
    /**
     * Gets the local IP address.
     * @return local IP address.
     * @throws IOException if error obtaining local IP address.
     */
    public static InetAddress getLocalAddress() throws IOException {
       
       InetAddress localAddress = InetAddress.getLocalHost();
       Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
       while (networks.hasMoreElements()) {
           NetworkInterface net = networks.nextElement();
           Enumeration<InetAddress> addresses = net.getInetAddresses();
           while (addresses.hasMoreElements()) {
               InetAddress address = addresses.nextElement();
               if ((address instanceof Inet4Address) && (!address.isLoopbackAddress()))
                   localAddress = address;
           }
       }
       
       return localAddress;
    }
}